let _id = location.search.substr(1);
// console.log(_id);

// 获取试卷信息

$.ajax({
  url: "/tests/getOneTests",
  type: "post",
  data: { _id: _id || "610bc6b7a50c000097006415" },
  success(res) {
    render(res);
    // 点击事件
    $(".startBtn").click(function () {
      let i = $(this).data("index");
      console.log(i);
      localStorage.testInfo = JSON.stringify(res[i]);
      location.href = "./start-test-1.html";
    });
  }
});

function render(arr) {
  console.log(arr);
  arr.forEach((v, index) => {
    $(".content").append(`
        <div class="item">
          <div class="item-row">
            <p>${v.title}</p>
          </div>
          <div class="item-row">
            <span>考试类型：</span>
            <span class="red">正式考试</span>
          </div>
          <div class="item-row">
            <span>考试地址：</span>
            <span class="red">信息中心</span>
          </div>
          <div class="item-row">
            <span>已考人数：</span>
            <span class="red">222</span>
          </div>
          <a class="item-row startBtn " href="#"
          data-index='${index}' testedid='${v._id}'
            >去练习</a
          >
        </div>
        `);
  });
  $(".username").text(userInfo.name);
  $(".img").attr("src", userInfo.avatar);
}

let testedArr;
$.ajax({
  url: "/tested/getTestResults",
  type: "post",
  success({ result }) {
    testedArr = result;
  }
});

//筛选当前学生考过得题目
testedArr = testedArr.filter((v) => v.studentId._id == userInfo._id);

console.log(testedArr);

testedArr.forEach((v) => {
  // 已考练习的Id
  let testedId = v.testId._id;
  // 找到要修改的元素
  $(`a[testedid='${testedId}']`)
    .addClass("grayBtn")
    .text("已完成 查看解析")
    .unbind()
    .on("click", function () {
      location.href = "./start-test-4.html";
      // alert("你已经做了");
    });
});
